
package com.tools.cleanmaster.store;

import android.text.TextUtils;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.utils.FeatureConfig;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 根据云端下发时间戳判断新、老用户的区分organic、非organic的开关通用解析
 */
public class CloudSwitchSharedPrefManager {

    private static final String JSON_KEY_NEW_ORGANIC_SWITCH = "new_organic_switch"; //organic新用户开关
    private static final String JSON_KEY_OLD_ORGANIC_SWITCH = "old_organic_switch"; //organic老用户开关
    private static final String JSON_KEY_OLD_NOT_ORGANIC_SWITCH = "old_not_organic_switch";//非organic老用户开关
    private static final String JSON_KEY_NEW_NOT_ORGANIC_SWITCH = "new_not_organic_switch";//非organic新用户开关
    private static final String JSON_JUDGE_OLD_NEW_TIME_STAMP = "stamp";//判断新老用户时间戳
    static final boolean DEFAULT_SWITCH = false;

    boolean newOrganicSwitch = DEFAULT_SWITCH;
    boolean oldOrganicSwitch = DEFAULT_SWITCH;
    boolean newNotOrganicSwitch = DEFAULT_SWITCH;
    boolean oldNotOrganicSwitch = DEFAULT_SWITCH;
    long judgeOldOrNewTimeStamp = 0;

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(", newOrganicSwitch :").append(newOrganicSwitch);
        builder.append(", oldOrganicSwitch :").append(oldOrganicSwitch);
        builder.append(", newNotOrganicSwitch: ").append(newNotOrganicSwitch);
        builder.append(", oldNotOrganicSwitch: ").append(oldNotOrganicSwitch);
        builder.append(", judgeOldOrNewTimeStamp: ").append(judgeOldOrNewTimeStamp);
        return builder.toString();
    }

    public static boolean getCloudSwitch(String json) {
        CloudSwitchSharedPrefManager configs = fromJson(json);
        if (GlobalSharedPrefManager.isNewUserByTimeStamp(configs.judgeOldOrNewTimeStamp)) {
            return GlobalSharedPrefManager.isOrganic(CleanApplication.getInstance()) ? configs.newOrganicSwitch : configs.newNotOrganicSwitch;
        }
        return GlobalSharedPrefManager.isOrganic(CleanApplication.getInstance()) ? configs.oldOrganicSwitch : configs.oldNotOrganicSwitch;
    }

    private static CloudSwitchSharedPrefManager fromJson(String json) {
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        // 在debug模式下如果JSON格式存在问题，抛出异常用于发现错误
        // 在非debug模式下如果JSON格式存在问题，回落到默认值
        try {
            return fromJsonImpl(new JSONObject(json));
        } catch (JSONException e) {
            if (FeatureConfig.DEBUG_LOG) {
                throw new RuntimeException(e);
            } else {
                return new CloudSwitchSharedPrefManager();
            }
        } catch (Throwable e) {
            if (FeatureConfig.DEBUG_LOG) {
                throw new RuntimeException(e);
            } else {
                return null;
            }
        }
    }

    private static CloudSwitchSharedPrefManager fromJsonImpl(JSONObject jo) {
        CloudSwitchSharedPrefManager configs = new CloudSwitchSharedPrefManager();
        configs.newOrganicSwitch = jo.optBoolean(JSON_KEY_NEW_ORGANIC_SWITCH, DEFAULT_SWITCH);
        configs.oldOrganicSwitch = jo.optBoolean(JSON_KEY_OLD_ORGANIC_SWITCH, DEFAULT_SWITCH);
        configs.newNotOrganicSwitch = jo.optBoolean(JSON_KEY_NEW_NOT_ORGANIC_SWITCH, DEFAULT_SWITCH);
        configs.oldNotOrganicSwitch = jo.optBoolean(JSON_KEY_OLD_NOT_ORGANIC_SWITCH, DEFAULT_SWITCH);
        configs.judgeOldOrNewTimeStamp = jo.optLong(JSON_JUDGE_OLD_NEW_TIME_STAMP, 0);
        return configs;
    }
}
